package maildao

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	cache "xr-game-server/core/cache"
	"xr-game-server/core/constants/common"
	"xr-game-server/entity"
)

var attachmentCache *cache.CacheMgr

func initAttachment() {
	attachmentCache = cache.NewCacheMgr()
}

func GetAttachment(mailId uint64) []*entity.Attachment {
	ret := attachmentCache.GetData(mailId, func(ctx context.Context) (value interface{}, err error) {
		db := make([]*entity.Attachment, common.Zero)
		g.Model(string(entity.TbAttachment)).Unscoped().Where(entity.AttachmentMailId, mailId).Scan(&db)
		return db, nil
	})
	return ret.([]*entity.Attachment)
}

func AddAttachment(attachment *entity.Attachment) {
	attachmentCache.FlushCache(attachment.MailId, attachment)
}
